package com.vueperson.vueperson.entity;

import lombok.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import javax.persistence.*;
import java.util.*;

@Entity
@Component
@Table(name = "vueUser02")

@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@EntityListeners(AuditingEntityListener.class)
public class User02 implements UserDetails {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "uid")
    private Integer id;
     String name;
     String address;
     String favorites;
     String email;
     String username;
     String password;
     @CreatedDate
     Long createTime;

    /**
     * 修改时间
     */
    @LastModifiedDate
    @Column(name = "modify_time")
    Date modifyTime;
     Boolean enabled;
     Boolean locked;

    /**
     * 多个用户对应一个角色
     * 注解形式配置多对一
     *  1,配置表关系
     *  2,配置外键
     */
//    @ManyToOne(cascade = CascadeType.PERSIST)
//    @JoinColumn(name = "rid")
    @ManyToMany
    @JoinTable(name = "role_to_user",
            joinColumns = @JoinColumn(name = "uid"),
            inverseJoinColumns = @JoinColumn(name = "rid"))
    Set<Role02> role02s=new HashSet<>();

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        for (Role02 r : role02s) {
            authorities.add(new SimpleGrantedAuthority(r.getName()));
        }
        return authorities;
    }

    //当前账户是否过期
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
    //当前账户是否未锁定
    @Override
    public boolean isAccountNonLocked() {
        return !locked;
    }
    //当前账户密码是否过期
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }
}
